package com.lijie.bs_recruit.Util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lijie.bs_recruit.model.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

//复杂的，被重复调用的wrapper工具类
@Component
public  class WrapperUtil {

    public QueryWrapper<Position> wrapperPosition(Position position){
        QueryWrapper<Position> wrapper =new QueryWrapper<>();
        if(position.getPositionId()!=null){
            wrapper.eq("positionId",position.getPositionId());
        }
        if(position.getCompanyName()!=null&& position.getCompanyName()!=""){
            wrapper.like("companyName",position.getCompanyName());
        }
        if(position.getHrId()!=null){
            wrapper.eq("hrId",position.getHrId());
        }
        if(position.getHrName()!=null&& position.getHrName()!=""){
            wrapper.like("hrName",position.getHrName());
        }
        if(position.getPositionName()!=null&& position.getPositionName()!=""){
            wrapper.like("positionName",position.getPositionName());
        }
        if(position.getEduRequirements()!=null&& position.getEduRequirements()!=""){
            wrapper.eq("eduRequirements",position.getEduRequirements());
        }
        if(position.getNeedNums()!=null ){
            wrapper.gt("needNums",0);
        }
        if(position.getJobNature()!=null&& position.getJobNature()!=""){
            wrapper.eq("jobNature",position.getJobNature());
        }
        if(position.getJobclassify()!=null && position.getJobclassify()!=""){
            wrapper.eq("jobClassify",position.getJobclassify());
        }
        if(position.getIscheck()!=null){
            wrapper.eq("ischeck",position.getIscheck());
        }
        if(position.getIsOpen()!=null){
            wrapper.eq("isOpen",position.getIsOpen());
        }
        if(position.getCity()!=null){
            wrapper.eq("city",position.getCity());
        }
        if(position.getMaxSalary()!=null){
            wrapper.le("maxSalary",position.getMaxSalary());
        }
        if(position.getMinSalary()!=null){
            wrapper.ge("minSalary",position.getMinSalary());
        }
        if(position.getWorkExp()!=null&&position.getWorkExp()!=""){
            wrapper.eq("workExp",position.getWorkExp());
        }
        if(position.getIsdeleted()!=null){
            wrapper.eq("isdeleted", position.getIsdeleted());
        }
        return wrapper;
    }

    public UpdateWrapper<Resume> resumeUpdateWrapper(Resume resume){
        UpdateWrapper<Resume> wrapper =new UpdateWrapper<>();
        wrapper.eq("userId",resume.getUserId());
        if(resume.getName()!=null){
            wrapper.set("name",resume.getName() );
        }
        if(resume.getSex()!=null){
            wrapper.set("sex",resume.getSex());
        }
        if(resume.getBirthdate()!=null){
            wrapper.set("birthdate", resume.getBirthdate());
        }
        if(resume.getMaxDegree()!=null){
            wrapper.set("maxDegree", resume.getMaxDegree());
        }
        if(resume.getTelephone()!=null){
            wrapper.set("telephone", resume.getTelephone());
        }
        if(resume.getEmail()!=null){
            wrapper.set("email", resume.getEmail());
        }
        if(resume.getSkill()!=null){
            wrapper.set("skill", resume.getSkill());
        }
        if(resume.getEvaluation()!=null){
            wrapper.set("evaluation", resume.getEvaluation());
        }
        if(resume.getHeaderImg()!=null){
            wrapper.set("headerImg", resume.getHeaderImg());
        }
        if(resume.getIsOpen()!=null){
            wrapper.set("isOpen", resume.getIsOpen());
        }
        if(resume.getResumefile()!=null){
            wrapper.set("resumefile", resume.getResumefile());
        }
        if(resume.getWorkExp()!=null){
            wrapper.set("workExp", resume.getWorkExp());
        }

        return wrapper;
    }

    public UpdateWrapper<Hr> hrUpdateWrapper(Hr hr){
        UpdateWrapper<Hr> hrUpdateWrapper=new UpdateWrapper<>();
        if (hr.getHrId()!=null){
            hrUpdateWrapper.eq("hrId", hr.getHrId());
        }
        if(hr.getCompanyId()!=null){
            hrUpdateWrapper.set("companyId",hr.getCompanyId());
        }
        if(hr.getCompanyName()!=null&&hr.getCompanyName()!=""){
            hrUpdateWrapper.set("companyName",hr.getCompanyName());
        }
        if(hr.getHrPassword()!=null&&hr.getHrPassword()!=""){
            hrUpdateWrapper.set("hrPassword",hr.getHrPassword());
        }
        if(hr.getHrTelephone()!=null&&hr.getHrTelephone()!=""){
            hrUpdateWrapper.set("hrTelephone",hr.getHrTelephone());
        }
        return hrUpdateWrapper;
    }

    public QueryWrapper<Company> wrapperBusiness(Company business){
        QueryWrapper<Company> wrapper =new QueryWrapper<>();
        if(business.getCompanyId()!=null){
            wrapper.eq("companyId", business.getCompanyId());
        }
        if(business.getAddress()!=null&& business.getAddress()!=""){
            wrapper.like("address", business.getAddress());
        }
        if(business.getCompanyName()!=null && business.getCompanyName()!=""){
            wrapper.like("companyName", business.getCompanyName());
        }
        if(business.getChairman()!=null && business.getChairman()!=""){
            wrapper.eq("chairman", business.getChairman());
        }
        if(business.getZctime()!=null){
            wrapper.eq("zctime", business.getZctime());
        }
        return wrapper;
    }

    public QueryWrapper<Hr> wrapperHr(Hr hr){
        QueryWrapper<Hr> wrapper =new QueryWrapper<>();
        if(hr.getHrId()!=null){
            wrapper.eq("hrId", hr.getHrId());
        }
        if(hr.getCompanyName()!=null &&hr.getCompanyName()!=""){
            wrapper.like("companyName", hr.getCompanyName());
        }
        if(hr.getName()!=null &&hr.getName()!=""){
            wrapper.like("name", hr.getName());
        }
        return wrapper;
    }
    public QueryWrapper<User> wrapperUser(User user){
        QueryWrapper<User> wrapper =new QueryWrapper<>();
        if(user.getUserId()!=null){
            wrapper.eq("userId", user.getUserId());
        }
        if(user.getUserName()!=null &&user.getUserName()!=""){
            wrapper.like("userName", user.getUserName());
        }
        if(user.getWorkExp()!=null &&user.getWorkExp()!=""){
            wrapper.like("workExp", user.getWorkExp());
        }
        if(user.getJobClassify()!=null &&user.getJobClassify()!=""){
            wrapper.like("jobClassify", user.getJobClassify());
        }
        return wrapper;
    }
    public QueryWrapper<Jobfair> wrapperJobfair(Jobfair jobfair){
        QueryWrapper<Jobfair> wrapper =new QueryWrapper<>();
        if(jobfair.getJobfairId()!=null){
            wrapper.eq("jobfairId", jobfair.getJobfairId());
        }
        if(jobfair.getJobfairName()!=null&&jobfair.getJobfairName()!=""){
            wrapper.eq("jobfairName", jobfair.getJobfairName());
        }
        if(jobfair.getAddress()!=null&&jobfair.getAddress()!=""){
            wrapper.eq("address", jobfair.getAddress());
        }
        if(jobfair.getStartTime()!=null){
            String start = DateFormatUtils.format(jobfair.getStartTime(),"yyyy-MM-dd HH:mm:ss");
            wrapper.apply("date_format (startTime,'%Y-%m-%d %H:%i:%s') >=date_format('" + start + "','%Y-%m-%d %H:%i:%s')");
        }
        if(jobfair.getStopTime()!=null){
            String stop = DateFormatUtils.format(jobfair.getStopTime(),"yyyy-MM-dd HH:mm:ss");
            wrapper.apply("date_format (stopTime,'%Y-%m-%d %H:%i:%s') <= date_format('" + stop + "','%Y-%m-%d %H:%i:%s')");
        }
        return wrapper;
    }

    public QueryWrapper<News> wrapperNews(News news){
        QueryWrapper<News> wrapper =new QueryWrapper<>();
        if(news.getNewsId()!=null){
            wrapper.eq("newsId", news.getNewsId());
        }
        if(news.getNewsTitle()!=null&&news.getNewsTitle()!=""){
            wrapper.like("newsTitle", news.getNewsTitle());
        }
        if(news.getCreatePerson()!=null&&news.getNewsTitle()!=""){
            wrapper.like("createPerson", news.getCreatePerson());
        }
        if(news.getIsDelete()!=null){
            wrapper.eq("isDelete", news.getIsDelete());
        }
        wrapper.orderByDesc("createTime","updateTime");
        return wrapper;
    }
}
